/**
 * 基于Jquery的select控件
 * @author wonderful-WuDaoHong wuketidai@foxmail.com
 * @create 20140521
 * @version 1.0.1
 * @description 
 */
 
(
function(){
	
	function getBrowserInfo(){
		var userAgent = navigator.userAgent.toLowerCase();
		// Figure out what browser is being used
		$.browser = {
		version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
		safari: /webkit/.test( userAgent ),
		opera: /opera/.test( userAgent ),
		msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
		mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
		};
	}
	//如果jquery版本大于1.9请执行此方法
	getBrowserInfo();
	
	var bro=$.browser;
    var binfo;
    if(bro.msie) {binfo=bro.version;}
    if(bro.mozilla) {binfo=bro.version;}
    if(bro.safari) {binfo=bro.version;}
    if(bro.opera) {binfo=version;}
    /**********************公共参数定义************************/
    /*var option={
	readOnly:true,//是否只读，不是必须
    fixedHeight:200,//固定下拉高度，内容超出高度显示滚动条，不是必须
    valName:"",//自定义值元素的name名称,唯恐则默认为渲染元素id值
    events:{//事件
    	onChange:function(obj,event,text,val){
    		
    	}
    }
};*/
   
    /**********************simple下拉模式相关操作函数************************/
    //$("#id").wdfComSel('addOpt',{text:,val:});
    //$("#id").wdfComSel('removeOpt',val);
    //$("#id").wdfComSel('getSel') retrun {val:'',text:''}
    //$("#id").wdfComSel('removeAll')
    //$("#id").wdfComSel('setVal',{text:"",val:""},triggerChange);
    //triggerChange (false/true)是否触发change事件
    //$("#id").wdfComSel('selVal',opt); opt:下拉索引从0开始  设置默认选中
    
    /**********************zTree下拉模式相关操作函数************************/
    //wdfComSel主要针对 zTree simpleData模式做很好的支持，建议使用zTree的simpleData模式   
    //$("#id").wdfComSel('zTreeSel',zTreeOption,buffOpt); 
       //zTreeOption zTree参数定义 
       /*
        buffopt={//附加参数
        	asyncRootNode:{//如果是异步加载模式，该项为zTree根节点对象，用于触发异步加载
        		id:"",
        		pId:"",
        		name:"",
        		isParent:false
        	},
        	convertKey:{//提供后台返回对象适应zTree节点对象属性的对应互转，此功能不能和zTree自带的相同功能同时使用
        	   pId:"parentID",
        	   name:"companyName",
        	   id:"no"
        	},
        	url:"",//数据请求地址,zTree非异步加载模式下的数据请求地址
        	containParent:true,//如果是多选节点，控件的val值是这些节点的id以逗号分隔的字符串，如果为true将包含父节点的id
            iconAppendBase:baseUrl,//个性icon的基础地址，即url前缀
            zTreeOnLoaded:function(zTreeObj,selObj){//树对象产生后触发
            
            }
        }   
         */
    
       //$("#id").wdfComSel('getZTree'); return zTree;直接返回zTree对象
       //$("#id").wdfComSel('getZTreeSel'); retrun {val:'',text:''}如果ztree是多选则会把选择的节点ID以‘,’号拼接成字符串
       //分隔作为val返回，text同理。
       //$("#id").wdfComSel("zTreeChecked",true/false);//设置下拉树的checkbox选中状态true为全部选中false反之
       //$("#id").wdfComSel("clearSelZtree");//清空下拉树的值和选中对象
      
   function stopBubble(e)  
   {  
       if (e && e.stopPropagation)  
           e.stopPropagation(); 
       else 
           window.event.cancelBubble=true;
   };
   
   $(document).bind("click",function(){
	   $(".optionList").css("display","none");
   });
   
	$.fn.extend({'wdfComSel':function(operat,opt,buffOpt){
		
		if(operat=='init')
			{
			var displayNones=[];
			this.parents().each(function(index){
				var display=$(this).css("display");
					$(this).css("display","block");
					displayNones.push({obj:$(this),display:display});
			});
			this.empty();
			
			/**
			 * 验证动态加入
			 * luomj 2014.9.12
			 * 1:今天表单验证css加入
			 */
			//TODO
			var validateClass=this.attr("class");
			var classVa='';
			if(undefined!=validateClass && null!=validateClass){
				if(validateClass.indexOf('validate')>=0){
					classVa=validateClass;
					//删除
					this.removeClass(classVa);
				}
			}
		    //获取其他css后进行
			var inputText=$("<input type='text' class='"+classVa+"'  id='"+this.attr("id")+"_WdfComSel_Text'/>");
			var inputVal=$("<input type='hidden'  id='"+this.attr("id")+"_WdfComSel_Val' name='"+this.attr("id")+"'/>");
			//保留其他css防止赋值处理
			this.addClass("comSel");
			this.append(inputText);
			this.append(inputVal);
			var contentObj=this;
			if(opt.readOnly&&opt.readOnly==true){
				inputText.attr("readonly",true);
			}
			if(opt.valName)
				{
				inputVal.attr("name",opt.valName);
				}
			if(opt.events)
				{
				 if(opt.events["onChange"])
					 {
					  var onChange=opt.events["onChange"];
					  var contentObj=this;
					  if(bro.mozilla){
						 
						  var userFun=function(obj,event){
							  onChange(obj,event,$(obj).val(),$(obj).parent().find("input[type='hidden']").val());
						  };
						  var inputCheckFun=function(){
								if(!inputCheck(contentObj)&&!opt.readOnly){
									inputVal.val(inputText.val());
								}
							};
							
							inputText.data("changeEvent",function(obj,event){
								userFun(obj,event);
								inputCheckFun();
							});
							
							inputText.attr("onchange","if (typeof($(this).data('changeEvent'))=='function')$(this).data('changeEvent')(this,event); ");
						  
					  }
					  else{
					  inputText.bind("change",function(){
						  onChange(this,event,$(this).val(),$(this).parent().find("input[type='hidden']").val());
					  });
					 
						inputText.bind("change",function(){
							if(!inputCheck(contentObj)&&!opt.readOnly){
								inputVal.val(inputText.val());
							}
						});
					  }
					 }
				}
			
			
			
			
		
			var selBtn=$("<div class='selBtn'></div>");
			this.append(selBtn);
			var inputFontSize=parseInt(inputText.css("font-size").replace(/px/g,""));
			var inputPaddingTop=parseInt(inputText.css("padding-top").replace(/px/g,""));
			var inputPaddingBottom=parseInt(inputText.css("padding-bottom").replace(/px/g,""));
			var inputPaddingLeft=parseInt(inputText.css("padding-left").replace(/px/g,""));
			var inputPaddingRight=parseInt(inputText.css("padding-right").replace(/px/g,""));
			var inputMarginTop=parseInt(inputText.css("margin-top").replace(/px/g,""));
			if(isNaN(inputMarginTop))
				{
				inputMarginTop=0;
				}
			
			var width=inputFontSize+inputPaddingTop+inputPaddingBottom;
			var height=width;
			var top=0;
			var left=0;
			var inputHeight=inputText.height();
			if(inputPaddingTop!=0)
				{
				inputHeight+=inputPaddingTop;
				}
			if(inputPaddingBottom!=0)
			    {
				inputHeight+=inputPaddingBottom;
			    }
				//parentIsDisplay(this.parent());
				
			/*alert(inputHeight);
			alert(this.height());*/
			left=inputText.width()+inputPaddingLeft+inputPaddingRight;
			selBtn.css({"left":left-29,"width":30,"height":inputHeight});
			
			selBtn.bind("click",function(e){
				var optLst=$(this).parent().find(".optionList");
				if(optLst.css("display")=='none'){
				optLst.css("display","block");
				}
				else{
					optLst.css("display","none");
				}
				$(".comSel").each(function(){
					if($(this).attr("id")==contentObj.attr("id"))
						return;
					$(this).parent().find(".optionList").css("display","none");
				});
				
				stopBubble(e);
			});
			
			selBtn.bind("mouseover",function(){
				$(this).addClass("selBtnChange");
			});
			selBtn.bind("mouseout",function(){
				$(this).removeClass("selBtnChange");
			});
			var optLst=this.find(".optionList");
			if(optLst.length==0)
				{
				$("<div class='optionList'></div>").appendTo(this);
				}
			optLst=this.find(".optionList");
			if(opt.fixedHeight)
				{
				optLst.css({"height":opt.fixedHeight,"overflow-y":"auto","overflow-x":"auto"});
				optLst.attr("fixedHeight",true);
				}
			else{
				optLst.css({"height":"auto","overflow-x":"auto"});
				optLst.attr("fixedHeight",false);
			}
			
			optLst.css({"left":0,"top":bro.mozilla?this.height()+2:this.height(),"width":inputText.width()+inputPaddingLeft+inputPaddingRight});
			optLst.find("div").each(function(){
				$(this).bind("click",function(){
					optClick(inputText,inputVal,this);
				});
			});
			
			$(displayNones).each(function(index){
				this.obj.css("display",this.display);
			});
			}
		else if(operat=='addOpt')
			{
			var optLst=this.find(".optionList");
			var option=$("<div val='"+opt.val+"' class='opt'>"+opt.text+"</div>");
			var inputText=this.find("input[type='text']");
			var inputVal=this.find("input[type='hidden']");
			option.bind("click",function(){
				optClick(inputText,inputVal,this);
			});
              if(bro.msie){
				option.bind("mouseover",function(){
					$(this).addClass("optHover");
				});
				option.bind("mouseout",function(){
					$(this).removeClass("optHover");
				});
			}
		    option.appendTo(optLst);
			
			}
		else if(operat=='removeOpt')
		{
		  this.find(".optionList").find("div").each(function(index){
			  if($(this).attr("val")==opt){
				  $(this).remove();
			  }
		  });
		}
		else if(operat=='getSel')
		{
		  var inputVal=this.find("input[type='hidden']");
		  var inputText=this.find("input[type='text']");
		  var selOption={text:'',val:''};
		  var isExists=false;
		  this.find(".optionList").find("div").each(function(index){
			  if($(this).attr("val")==inputVal.val()){
				 selOption["text"]=$(this).text();
				 selOption["val"]=$(this).attr('val');
				 isExists=true;
			  }
		  });
		  if(!isExists)
			  {
			  selOption.val=inputVal.val();
			  selOption.text=inputText.val();
			  }
		return selOption;
		}
		else if(operat=='removeAll')
		{
			this.find(".optionList").find("div").remove();
		}
		else if(operat=='setVal')
		{
		  var inputVal=this.find("input[type='hidden']");
		  var inputText=this.find("input[type='text']");
		  var selOption={text:'',val:''};
		  var optLst=this.find(".optionList");
		  if(buffOpt){
		  setTimeout(function(){
			  inputText.val(opt.text);
			  inputText.triggerHandler('change');
		  },200);
		  }
		  else{
			  //inputVal.val(opt.val);
			  setTimeout(function(){
				  inputText.val(opt.text);
			  },200);
		  }
		  inputVal.val(opt.val);
		  var isChoice=false;
		  optLst.find("div").each(function(index){

			  if(($(this).text()==opt.text&&$(this).attr("val")==opt.val)||$(this).attr("val")==opt.val)  
			    {
				optLst.find(".choice").removeClass("choice");
				$(this).addClass("choice");
				inputVal.val(opt.val);
				inputText.val($(this).text());
				isChoice=true;
			    }
		    
		  });
		  if(!isChoice){
			  optLst.find(".choice").removeClass("choice");
		  }
		 
		}
		else if(operat=='selVal')
		{
		  var inputVal=this.find("input[type='hidden']");
		  var inputText=this.find("input[type='text']");
		  var selOption={text:'',val:''};
		  var optLst=this.find(".optionList");
		  
		  optLst.find("div").each(function(index){

			  if(opt==index)  
			    {
				inputVal.val($(this).attr("val"));
				inputText.val($(this).text());
				optLst.find(".choice").removeClass("choice");
				$(this).addClass("choice");
			    }
		    
		  });
		   
		}
     /**----------------------------zTreeSelMode--------------------------------**/
		else if(operat=='zTreeSel'){
			var zTreeOpt={};
			var zTree;
			var selObj=this;
			var optionList=this.find(".optionList");
			$.fn.extend(true,zTreeOpt,opt);
			$("#"+this.attr("id")+"_ZTree").remove();
			var treeContent=$("<ul id='"+this.attr("id")+"_ZTree' class='ztree'></ul>");
			treeContent.bind("click",function(e){
				stopBubble(e);
			});
			if(optionList.attr("fixedHeight")=="false"){
				treeContent.css({"height":"auto","width":optionList.width(),"overflow-x":"auto","overflow-y":"","padding":"0px"});
				if(bro.msie&&bro.version>7){
				treeContent.bind("mouseover",function(){
					
					treeContent.css({"height":treeContent.find(".level0").height()+18});
					
				});}
				optionList.css({"height":"auto","padding":"0px","overflow-x":"","overflow-y":""});
			}
			else{
				treeContent.css({"height":optionList.height(),"overflow":"auto","width":optionList.width(),"padding":"0px"});
				optionList.css({"height":"auto","overflow-y":"","overflow-x":"","padding":"0px"});
			}
			
			optionList.append(treeContent);
			if(opt.check&&opt.check.enable)
				{
				var okBtn=$("<a></a>");
				var btnContent=$("<div class='btnContent'></div>");
				okBtn.click(function(e){
				var nodes=zTree.getCheckedNodes(true);
				var val=selObj.find("input[type='hidden']");
				var text=selObj.find("input[type='text']");
				val.val("");
				text.val("");
				var tempVal="";
				var tempText="";
				$(nodes).each(function(){
					
					if(!buffOpt.containParent&&this.isParent){
						return;
					}
					else{
						 tempVal+=this.id+",";
						 tempText+=this.name+",";
						
					}
				});
				
				tempVal=tempVal.substring(0,tempVal.lastIndexOf(","));
				tempText=tempText.substring(0,tempText.lastIndexOf(","));
			    text.val(tempText);	
		        val.val(tempVal);	
				});
				
				if(bro.msie){
					okBtn.bind("mouseover",function(){
						$(this).addClass("okBtnHover");
					});
					okBtn.bind("mouseout",function(){
						$(this).removeClass("okBtnHover");
					});
				}
				btnContent.append(okBtn);
				optionList.append(btnContent);
				
				}
			else{
				
				if(zTreeOpt.callback){
					if(typeof(zTreeOpt.callback.onClick)=="function"){
					
						var userFun=zTreeOpt.callback.onClick;
						zTreeOpt.callback.onClick=function(event,treeID,treeNode){
							var nodes=zTree.getCheckedNodes(true);
							var val=selObj.find("input[type='hidden']");
							var text=selObj.find("input[type='text']");
							val.val(treeNode.id);
							text.val(treeNode.name);
							optionList.css("display","none");
							userFun(event,treeID,treeNode);
						};
					}
					else{
						zTreeOpt.callback.onClick=function(event,treeID,treeNode){
							var nodes=zTree.getCheckedNodes(true);
							var val=selObj.find("input[type='hidden']");
							var text=selObj.find("input[type='text']");
							val.val(treeNode.id);
							text.val(treeNode.name);
						};
					}
					}
				else{
					zTreeOpt.callback={};
					zTreeOpt.callback.onClick=function(event,treeID,treeNode){
						var nodes=zTree.getCheckedNodes(true);
						var val=selObj.find("input[type='hidden']");
						var text=selObj.find("input[type='text']");
						val.val(treeNode.id);
						text.val(treeNode.name);
					};
					
				}
				
			}
			
			if(opt.async&&opt.async.enable)
			{
			
			zTree=$.fn.zTree.init(treeContent,zTreeOpt,buffOpt.asyncRootNode);
			if(buffOpt.zTreeOnLoaded)
			buffOpt.zTreeOnLoaded(zTree,selObj);
			}
			else{
				
				var ajaxOpt={
						url :buffOpt.url,
						//processData: false,
						//data: xmlDocument,
						async : true,
						dataType : "json",
						type : "POST",
						contentType : "application/x-www-form-urlencoded",
						data :{},
						beforeSend : null,
						success : function(data) {
							var zTreeDataArray=[];
							if(buffOpt.convertKey){
								
								$(data).each(function(){
									var zTreeData={};
									if(buffOpt.convertKey.id)
										{
										zTreeData.id=this[buffOpt.convertKey.id];
										}
									else{
										zTreeData.id=this["id"];
									    }
									if(buffOpt.convertKey.pId)
									{
									zTreeData.pId=this[buffOpt.convertKey.pId];
									}
								else{
									zTreeData.pId=this["pId"];
								    }
									if(buffOpt.convertKey.name)
									{
									zTreeData.name=this[buffOpt.convertKey.name];
									}
								else{
									zTreeData.name=this["name"];
								    }
								if(buffOpt.convertKey.icon){
									if(buffOpt.iconAppendBase)
									{
									zTreeData.icon=buffOpt.iconAppendBase+"/"+this[buffOpt.convertKey.icon];
									}
									else{
										zTreeData.icon=this[buffOpt.convertKey.icon];
									}
								}
								else{
									if(buffOpt.iconAppendBase&&this.icon)
									{
									zTreeData.icon=buffOpt.iconAppendBase+"/"+this.icon;
									}
								}
								
								
									zTreeDataArray.push(zTreeData);
								});
							}
							else{
								
								zTreeDataArray=data;
								if(buffOpt.iconAppendBase){
									$(zTreeDataArray).each(function(i){
										if(this.icon)
										this.icon=buffOpt.iconAppendBase+"/"+this.icon;
									});
								}
							}
							zTree=$.fn.zTree.init(treeContent,zTreeOpt,zTreeDataArray);
							if(buffOpt.zTreeOnLoaded)
							buffOpt.zTreeOnLoaded(zTree,selObj);
						},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
							  
							alert(textStatus);

						}
					};
				$.ajax(ajaxOpt);
			}
			
		}
		else if(operat=='getZtreeSel')
		{
		  var val=this.find("input[type='hidden']");
		  var text=this.find("input[type='text']");
		  var selOption={text:'',val:''};
				 selOption["text"]=text.val();
				 selOption["val"]=val.val();
		return selOption;
		}
		else if(operat=='getZtree')
		{
	    var treeObj = $.fn.zTree.getZTreeObj(this.attr("id")+"_ZTree");
		return treeObj;
		}
		else if(operat=='zTreeChecked')
		{
	    var treeObj = $.fn.zTree.getZTreeObj(this.attr("id")+"_ZTree");
	    treeObj.checkAllNodes(opt);
		}
		else if(operat=='clearSelZtree')
		{
	    var treeObj = $.fn.zTree.getZTreeObj(this.attr("id")+"_ZTree");
	    treeObj.checkAllNodes(false);
	    	var val=this.find("input[type='hidden']");
			var text=this.find("input[type='text']");
			var val=this.find("input[type='hidden']");
			text.val("");
			val.val("");
		}
		
	}});
	
	function optClick(inputText,inputVal,obj){
		
		$(inputText).val($(obj).text());
		$(inputVal).val($(obj).attr("val"));
		$(obj).parent().css("display","none");
		if(!bro.mozilla){
			inputText.triggerHandler('change');
		}else{
			inputText.triggerHandler('change');
		}
		$(obj).parent().find(".choice").removeClass("choice");
		$(obj).addClass("choice");
	}
	
	function inputCheck(contentObj){
		var inputText=contentObj.find("input[type='text']");
		var inputVal=contentObj.find("input[type='hidden']");
		var optLst=contentObj.find(".optionList");
		var isExists=false;
		optLst.find("div").each(function(index){
			if(inputText.val()==$(this).text()){
				isExists=true;
			}
		});
	return isExists;
	}
	
	function getZtreeData(data,target){
		
		var topMenu={
		};
		topMenu.id=data.attributes.id;
		topMenu.pId=data.attributes.parentId;
		topMenu.name=data.attributes.text;

		topMenu.open=true;
		var ztreeDataArray=[];
		ztreeDataArray.push(topMenu);
		if(data.children&&data.children.length>0)
			{
			
			childrenProc(data.children,ztreeDataArray,target);
			
			}
		
	   
		function childrenProc(children,ztreeDataArray,target){
			
			if(children.length>0)
			{
			   for(var i=0;i<children.length;i++){
				  var protoMenu=children[i].attributes;
				  var ztreeMenu={};
				    ztreeMenu.id=protoMenu.id;
					ztreeMenu.pId=protoMenu.parentId;
					ztreeMenu.name=protoMenu.text;
					ztreeMenu.target=target;
					var url=(null==protoMenu.pageUrl)?"":protoMenu.pageUrl;
					ztreeMenu.url=url;
					ztreeDataArray.push(ztreeMenu);
				    if(children[i].children){
				    	childrenProc(children[i].children,ztreeDataArray,target);
				    }
					
			   }
			}
		}
		return ztreeDataArray;
	}


	/******************************UUID********************************/
		/*

		uuid.js - Version 0.2
		JavaScript Class to create a UUID like identifier

		Copyright (C) 2006-2008, Erik Giberti (AF-Design), All rights reserved.

		This program is free software; you can redistribute it and/or modify it under 
		the terms of the GNU General Public License as published by the Free Software 
		Foundation; either version 2 of the License, or (at your option) any later 
		version.

		This program is distributed in the hope that it will be useful, but WITHOUT ANY 
		WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
		PARTICULAR PURPOSE. See the GNU General Public License for more details.

		You should have received a copy of the GNU General Public License along with 
		this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
		Place, Suite 330, Boston, MA 02111-1307 USA

		The latest version of this file can be downloaded from
		http://www.af-design.com/resources/javascript_uuid.php

		HISTORY:
		6/5/06 	- Initial Release
		5/22/08 - Updated code to run faster, removed randrange(min,max) in favor of
		          a simpler rand(max) function. Reduced overhead by using getTime() 
		          method of date class (suggestion by James Hall).

		KNOWN ISSUES:
		- Still no way to get MAC address in JavaScript
		- Research into other versions of UUID show promising possibilities 
		  (more research needed)
		- Documentation needs improvement

		*/

		// On creation of a UUID object, set it's initial value
		function UUID(){
			this.id = this.createUUID();
		}

		// When asked what this Object is, lie and return it's value
		UUID.prototype.valueOf = function(){ return this.id; }
		UUID.prototype.toString = function(){ return this.id; }

		//
		// INSTANCE SPECIFIC METHODS
		//

		UUID.prototype.createUUID = function(){
			//
			// Loose interpretation of the specification DCE 1.1: Remote Procedure Call
			// described at http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37
			// since JavaScript doesn't allow access to internal systems, the last 48 bits 
			// of the node section is made up using a series of random numbers (6 octets long).
			//  
			var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
			var dc = new Date();
			var t = dc.getTime() - dg.getTime();
			var h = '-';
			var tl = UUID.getIntegerBits(t,0,31);
			var tm = UUID.getIntegerBits(t,32,47);
			var thv = UUID.getIntegerBits(t,48,59) + '1'; // version 1, security version is 2
			var csar = UUID.getIntegerBits(UUID.rand(4095),0,7);
			var csl = UUID.getIntegerBits(UUID.rand(4095),0,7);

			// since detection of anything about the machine/browser is far to buggy, 
			// include some more random numbers here
			// if NIC or an IP can be obtained reliably, that should be put in
			// here instead.
			var n = UUID.getIntegerBits(UUID.rand(8191),0,7) + 
					UUID.getIntegerBits(UUID.rand(8191),8,15) + 
					UUID.getIntegerBits(UUID.rand(8191),0,7) + 
					UUID.getIntegerBits(UUID.rand(8191),8,15) + 
					UUID.getIntegerBits(UUID.rand(8191),0,15); // this last number is two octets long
			return tl + h + tm + h + thv + h + csar + csl + h + n; 
		}


		//
		// GENERAL METHODS (Not instance specific)
		//


		// Pull out only certain bits from a very large integer, used to get the time
		// code information for the first part of a UUID. Will return zero's if there 
		// aren't enough bits to shift where it needs to.
		UUID.getIntegerBits = function(val,start,end){
			var base16 = UUID.returnBase(val,16);
			var quadArray = new Array();
			var quadString = '';
			var i = 0;
			for(i=0;i<base16.length;i++){
				quadArray.push(base16.substring(i,i+1));	
			}
			for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){
				if(!quadArray[i] || quadArray[i] == '') quadString += '0';
				else quadString += quadArray[i];
			}
			return quadString;
		}

		// Numeric Base Conversion algorithm from irt.org
		// In base 16: 0=0, 5=5, 10=A, 15=F
		UUID.returnBase = function(number, base){
			//
			// Copyright 1996-2006 irt.org, All Rights Reserved.	
			//
			// Downloaded from: http://www.irt.org/script/146.htm	
			// modified to work in this class by Erik Giberti
			var convert = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
		    if (number < base) var output = convert[number];
		    else {
		        var MSD = '' + Math.floor(number / base);
		        var LSD = number - MSD*base;
		        if (MSD >= base) var output = this.returnBase(MSD,base) + convert[LSD];
		        else var output = convert[MSD] + convert[LSD];
		    }
		    return output;
		}

		// pick a random number within a range of numbers
		// int b rand(int a); where 0 <= b <= a
		UUID.rand = function(max){
			return Math.floor(Math.random() * max);
		}

		// end of UUID class file
	/*************************************************************************/
	
	
}

)
()